package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.HxxkMapper;
import com.hbnu.system.model.entity.Hxxk;
import com.hbnu.system.service.HxxkService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 横向科技类别(Hxxk)表服务实现类
 *
 * @author makejava
 * @since 2024-12-20 14:49:16
 */
@Service
public class HxxkServiceImpl extends ServiceImpl<HxxkMapper, Hxxk> implements HxxkService {
    @Override
    public boolean addHxxk(Hxxk hxxk) {
        Hxxk projectcate1 = super.getOne(Wrappers.lambdaQuery(Hxxk.class)
                .eq(Hxxk::getName,hxxk.getName()));
        if(StrUtil.isEmpty(hxxk.getName())||StrUtil.isBlank(hxxk.getName())){
            throw new BaseException("该项目类别名称为空");
        }
        if(!Objects.isNull(projectcate1)){
            throw new BaseException("该学科分类名称已存在");
        }
        return baseMapper.insert(hxxk)==1;
    }

    @Override
    public List<Hxxk> getHxxkList() {
        List<Hxxk> depts = baseMapper.selectList(Wrappers.lambdaQuery(Hxxk.class));
//        List<String> result = new ArrayList<>();
//        for (Dept dept : depts) {
//            result.add(dept.getDeptName());
//        }
        return depts;
    }

    @Override
    public boolean updateHxxk(Hxxk hxxk) {
        if(StrUtil.isNotEmpty(hxxk.getName())){
            Hxxk projectcate1 = super.getOne(Wrappers.lambdaQuery(Hxxk.class)
                    .eq(Hxxk::getName,hxxk.getName()));
            if(!Objects.isNull(projectcate1)){
                throw new BaseException("该学科分类名称已存在");
            }
            return baseMapper.updateById(hxxk)==1;
        }
        return false;
    }

    @Override
    public boolean deleteHxxk(String ID) {
        return baseMapper.deleteById(ID) == 1;
    }

    @Override
    public List<Hxxk> getByHxxkName(String Name) {
        LambdaQueryWrapper<Hxxk> queryWrapper = Wrappers.lambdaQuery(Hxxk.class);

        // 如果 disciplineClassfiName 不为空且不是空白字符串，则添加模糊匹配条件
        if (StringUtils.isNotBlank(Name)) {
            queryWrapper.like(Hxxk::getName, Name);
        }

        return baseMapper.selectList(queryWrapper);
    }


}
